package com.yzk.aigy.controller;

import com.yzk.aigy.common.OssTemplate;
import com.yzk.aigy.common.Result;
import com.yzk.aigy.common.VodTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@Api(tags = "视频上传控制器")
@RequiredArgsConstructor
@RequestMapping("/admin/system/upload")
@CrossOrigin("*")  // 解决跨域问题
public class UploadController {

    private final OssTemplate ossTemplate;
    private final VodTemplate vodTemplate;

    @ApiOperation("上传图片")
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile uploadImage) throws IOException {
        String imageUrl = ossTemplate.upload(uploadImage.getOriginalFilename(), uploadImage.getInputStream());
        log.info("文件上传路径：{}", imageUrl);
        return Result.ok(imageUrl);
    }

    @ApiOperation("上传视频")
    @PostMapping("/uploadVideo")
    public Result<String> uploadVideo(MultipartFile uploadVideo) throws IOException {
        String videoId = this.vodTemplate.uploadVideo(uploadVideo.getOriginalFilename(), uploadVideo.getInputStream());
        log.info("视频上传ID：{}", videoId);
        return Result.ok(videoId);
    }

}
